package com.yunxin.core.infra.persistence.repository.merchant;

import com.yunxin.core.domain.model.merchant.MerchantInfo;
import com.yunxin.core.domain.model.merchant.MerchantInfoRepository;
import com.yunxin.core.infra.persistence.converter.MerchantConverter;
import com.yunxin.core.infra.persistence.entity.merchant.MerchantInfoDO;
import com.yunxin.core.infra.persistence.mapper.merchant.MerchantInfoExtendMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

/**
 * @author zero°
 * @since 2023-07-12
 */
@Repository
@RequiredArgsConstructor
public class MerchantInfoRepositoryImpl implements MerchantInfoRepository {

    private final MerchantInfoExtendMapper merchantInfoExtendMapper;

    private final MerchantConverter merchantConverter;
    @Override
    public boolean saveMerchantInfo(MerchantInfo merchantInfo) {
        MerchantInfoDO merchantInfoDO = merchantConverter.formMerchantInfo(merchantInfo);
        int count = merchantInfoExtendMapper.insertSelective(merchantInfoDO);
        merchantInfo.setId(merchantInfoDO.getId());
        return count > 0;
    }

    @Override
    public boolean updateMerchantInfo(MerchantInfo merchantInfo) {
        MerchantInfoDO merchantInfoDO = merchantConverter.formMerchantInfo(merchantInfo);
        int count = merchantInfoExtendMapper.updateByPrimaryKeySelective(merchantInfoDO);
        return count > 0;
    }

    @Override
    public MerchantInfo getMerchantInfoById(Long id) {
        MerchantInfoDO merchantInfoDO = merchantInfoExtendMapper.selectByPrimaryKey(id);
        return merchantConverter.toMerchantInfo(merchantInfoDO);
    }
}
